<script lang="ts" setup>
import { ref, watch, computed, type PropType } from 'vue';
import { Pagination, type PaginationPageChangeDetails } from '@ark-ui/vue/pagination';
import { useRouter } from 'vue-router';

const props = defineProps({
  count: {
    type: Number,
    default: 0,
  },

  pageSize: {
    type: Number,
    default: 20,
  },

  page: {
    type: Number,
    default: 1,
  },

  siblingCount: {
    type: Number,
    default: 2,
  },

  gotoPage: {
    type: Boolean,
    default: false,
  },

  getRouterPath: {
    type: Function as PropType<(offset: number) => string>,
    default: null,
  },
});

const router = useRouter();

const emit = defineEmits(['change']);
const pageNumber = ref('');
const pageValue = ref(props.page);
const countValue = ref(props.count);

const totalPagesValue = computed(() => Math.ceil(countValue.value / props.pageSize));

const handlePageChange = (currentPage: PaginationPageChangeDetails) => {
  emit('change', currentPage.page);
};

const goto = () => {
  const inputPage = Math.floor(Number(pageNumber.value));

  if (!inputPage || inputPage > totalPagesValue.value || inputPage < 1) {
    pageNumber.value = '';
    return;
  }

  pageValue.value = inputPage;

  const path = props.getRouterPath?.(inputPage);

  if (path) {
    router.push(path);
  }

  emit('change', inputPage);

  pageNumber.value = '';
};

watch(() => props.page, (value) => {
  pageValue.value = value;
});

watch(() => props.count, (value) => {
  countValue.value = value;
});
</script>

<template>
  <Pagination.Root
    class="flex-center text-14"
    :page="pageValue"
    :count="countValue"
    :page-size="props.pageSize"
    :sibling-count="props.siblingCount"
    :data-total-pages="totalPagesValue > 1 ? 'gt1' : 'lt1'"
    @page-change="handlePageChange"
  >
    <Pagination.Context v-slot="{ page: currentPage, pages, totalPages, previousPage, nextPage }">
      <template v-if="totalPages > 1">
        <div class="flex gap-x-8">
          <Pagination.PrevTrigger as-child>
            <RouterLink :to="props.getRouterPath(previousPage ?? 1)" class="size-32 flex-center rounded-6 bg-white text-#345DE2 data-[disabled]:(cursor-not-allowed text-#CECECE)" v-if="!!props.getRouterPath && currentPage > 1">
              <span class="i-icons:arrow-left block size-16"></span>
            </RouterLink>
            <button type="button" class="size-32 flex-center rounded-6 bg-white text-#345DE2 data-[disabled]:(cursor-not-allowed text-#CECECE)" v-else>
              <span class="i-icons:arrow-left block size-16"></span>
            </button>
          </Pagination.PrevTrigger>

          <template v-for="(item, index) in pages">
            <Pagination.Item as-child :type="item.type" :key="index" :value="item.value" v-if="item.type === 'page'">
              <RouterLink :to="props.getRouterPath(item.value)" class="h-32 min-w-32 flex-center border-1 border-transparent rounded-6 border-solid bg-white text-#6E6E6E data-[selected]:(cursor-default border-#E8F0FF bg-#E8F0FF text-#345DE2 font-medium)" v-if="!!props.getRouterPath && currentPage != item.value">
                {{ item.value }}
              </RouterLink>
              <button type="button" class="h-32 min-w-32 border-1 border-transparent rounded-6 border-solid bg-white text-#6E6E6E data-[selected]:(cursor-default border-#E8F0FF bg-#E8F0FF text-#345DE2 font-medium)" v-else>
                {{ item.value }}
              </button>
            </Pagination.Item>
            <Pagination.Ellipsis class="h-32 flex-center" :key="'e' + index" :index="index" v-else>...</Pagination.Ellipsis>
          </template>

          <Pagination.NextTrigger as-child>
            <RouterLink :to="props.getRouterPath(nextPage ?? totalPages)" class="size-32 flex-center rounded-6 bg-white text-#345DE2 data-[disabled]:(cursor-not-allowed text-#CECECE)" v-if="!!props.getRouterPath && currentPage < totalPages">
              <span class="i-icons:arrow-right block size-16"></span>
            </RouterLink>
            <button type="button" class="size-32 flex-center rounded-6 bg-white text-#345DE2 data-[disabled]:(cursor-not-allowed text-#CECECE)" v-else>
              <span class="i-icons:arrow-right block size-16"></span>
            </button>
          </Pagination.NextTrigger>
        </div>

        <template v-if="props.gotoPage">
          <div class="ml-16 h-32 flex-y-center">
            <div class="color-hex-292929 leading-32">{{ currentPage }}</div>
            <div class="color-hex-6e6e6e leading-32">/{{ totalPages }}</div>
          </div>

          <form @submit.prevent="goto" class="ml-16 flex">
            <i18n-t keypath="home.component.pagination.goto" scope="global" tag="div" class="color-hex-6e6e6e leading-32">
              <template #component>
                <input class="mx-8 h-32 w-50 border-1 border-#e9edf0 rounded-6 px-12 focus:(border-#1b9aee outline-none) hover:(border-#1b9aee)" v-model="pageNumber">
              </template>
            </i18n-t>

            <button
              type="submit"
              class="ml-8 h-32 rounded-6 bg-#f2f5f7 px-16 text-center color-#575757 hover:(bg-#dae0e5 color-#292929)"
            >
              {{ $t('home.component.pagination.ok') }}
            </button>
          </form>
        </template>
      </template>
    </Pagination.Context>
  </Pagination.Root>
</template>

<style lang="scss" scoped>
</style>
